package com.across.connect.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.across.connect.home.PlanHome;
import com.across.connect.home.MyUserHome;
import com.across.connect.model.FriendPlanView;
import com.across.connect.model.MyUser;
import com.across.connect.model.Plan;
import com.across.connect.util.XnConnect;

/**
 * MyEclipse Struts Creation date: 04-15-2009
 * 
 * XDoclet definition:
 * 
 * @struts.action validate="true"
 */
public class HomeAction extends AbstractAction {
	/*
	 * Generated Methods
	 */
	private static final String FORWARD_HOME = "succ";

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward handleLogic(ActionMapping mapping,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		if (getHost().getXnuid() > 0) {
			// 如果是校内用户 则显示好友的计划信息
			List<Integer> firendsId = XnConnect.getXiaoneiAPPFriends(getHost()
					.getXnuid());
			if (firendsId != null && firendsId.size() > 0) {
				List<FriendPlanView> friendsPlan = new ArrayList<FriendPlanView>();
				for (Integer id : firendsId) {
					MyUser usr = MyUserHome.getInstance()
							.getMyUserByXiaoneiUID(id);
					FriendPlanView view = new FriendPlanView();
					view.setXnuid(id);
					if (usr == null) {
						// 不应该在本地获取不到用户信息 但有时也可能因为某些原因导致Connect用户的数据没有安装到本地数据库中
						view.setContent("No swim ... yet.");
					} else {
						view.setId(usr.getId());
						view.setName(usr.getName());
						Plan plan = PlanHome.getInstance()
								.getLatestPlantByUserID(usr.getId());
						if (plan != null) {
							view.setContent(plan.getContent());
							view.setDate(plan.getDate());
						} else {
							view.setContent("No swim ... yet.");
						}
					}
					friendsPlan.add(view);
				}
				request.setAttribute("friendPlans", friendsPlan);
			}
		}

		return mapping.findForward(FORWARD_HOME);
	}


}